package com.telchina.workorder.third.service.impl;


import cn.hutool.db.PageResult;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.telchina.common.core.domain.AjaxResult;
import com.telchina.common.utils.SecurityUtils;
import com.telchina.workorder.third.config.BpmConfig;
import com.telchina.workorder.third.service.BpmTaskThirdService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


/**
 * 流程任务实例 Service 实现类
 *
 * @author jason
 * @author telchina
 */
@Slf4j
@Service
public class BpmTaskThirdServiceImpl implements BpmTaskThirdService {

    @Autowired
    private BpmConfig bpmConfig;

    @Override
    public AjaxResult getTodoTask(Map<String, Object> paramsMap) {
        log.info("开始调用远程服务{},参数为：{}", "getTodoTask", paramsMap);
        // 获取请求的地址
        String sendUrl = bpmConfig.getBaseUrl() + bpmConfig.getGetTodoTaskApi();
        String postResult = HttpUtil.get(sendUrl, paramsMap);
        log.info("结束调用远程服务{},返回结果为：{}", "getTodoTask", postResult);
        AjaxResult ajaxResult = JSON.parseObject(postResult, AjaxResult.class);
        return ajaxResult;
    }

    /**
     * 获得指定流程实例的任务列表
     *
     * @param processInstanceId
     * @return
     */
    @Override
    public AjaxResult getTaskListByProcessInstanceId(String processInstanceId) {
        log.info("开始调用远程服务{},参数为：{}", "getTaskListByProcessInstanceId", processInstanceId);
        // 获取请求的地址
        String sendUrl = bpmConfig.getBaseUrl() + bpmConfig.getTaskListByProcessInstanceIdApi();
        sendUrl += "?processInstanceId="+ processInstanceId;
        String postResult = HttpUtil.get(sendUrl);
        log.info("结束调用远程服务{},返回结果为：{}", "getTaskListByProcessInstanceId", postResult);
        AjaxResult ajaxResult = JSON.parseObject(postResult, AjaxResult.class);
        return ajaxResult;
    }

    /**
     * 当前人员是否能处理这个任务
     *
     * @param paramsMap
     * @return
     */
    @Override
    public AjaxResult checkUserIdDoTask(String taskId) {
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("userId", SecurityUtils.getUserId());
        paramsMap.put("id", taskId);
        log.info("开始调用远程服务{},参数为：{}", "checkUserIdDoTask", paramsMap);
        // 获取请求的地址
        String sendUrl = bpmConfig.getBaseUrl() + bpmConfig.getCheckUserIdDoTaskApi();
        String postResult = HttpUtil.post(sendUrl, JSON.toJSONString(paramsMap));
        log.info("结束调用远程服务{},返回结果为：{}", "checkUserIdDoTask", postResult);
        AjaxResult ajaxResult = JSON.parseObject(postResult, AjaxResult.class);
        return ajaxResult;
    }

    /**
     * 获取流程定义时属性
     *
     * @param taskId
     * @param customPropertyName
     */
    @Override
    public AjaxResult getCustomProperty(String taskId, String customPropertyName) {
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("customPropertyName", customPropertyName);
        paramsMap.put("taskId", taskId);
        log.info("开始调用远程服务{},参数为：{}", "getCustomProperty", paramsMap);
        // 获取请求的地址
        String sendUrl = bpmConfig.getBaseUrl() + bpmConfig.getCustomPropertyApi();
        String postResult = HttpUtil.get(sendUrl, paramsMap);
        log.info("结束调用远程服务{},返回结果为：{}", "getCustomProperty", postResult);
        AjaxResult ajaxResult = JSON.parseObject(postResult, AjaxResult.class);
        return ajaxResult;
    }

    /**
     * 获取下一节点的任务属性
     *
     * @param mapVar
     */
    @Override
    public AjaxResult getNextUserTask(Map<String, Object> mapVar) {
        log.info("开始调用远程服务{},参数为：{}", "getNextUserTask", mapVar);
        // 获取请求的地址
        String sendUrl = bpmConfig.getBaseUrl() + bpmConfig.getNextTaskApi();
        String postResult = HttpUtil.post(sendUrl, JSON.toJSONString(mapVar));
        log.info("结束调用远程服务{},返回结果为：{}", "getNextUserTask", postResult);
        return JSON.parseObject(postResult, AjaxResult.class);
    }

    /**
     * 后去下一步操作节点（配置按钮）
     *
     * @param processInstanceId
     */
    @Override
    public AjaxResult getNextFlow(String processInstanceId) {
        log.info("开始调用远程服务{},参数为：{}", "getNextFlow", processInstanceId);
        // 获取请求的地址
        String sendUrl = bpmConfig.getBaseUrl() + bpmConfig.getNextFlowApi() + "?processInstanceId=" + processInstanceId;
        String postResult = HttpUtil.get(sendUrl);
        log.info("结束调用远程服务{},返回结果为：{}", "getNextFlow", postResult);
        return JSON.parseObject(postResult, AjaxResult.class);
    }

    /**
     * 配置按钮
     *
     * @param taskDefinitionkey
     */
    @Override
    public AjaxResult getButton(String taskDefinitionkey) {
        log.info("开始调用远程服务{},参数为：{}", "getButton", taskDefinitionkey);
        // 获取请求的地址
        String sendUrl = bpmConfig.getBaseUrl() + bpmConfig.getButtonApi() + "?definitionKey=" + taskDefinitionkey;
        String postResult = HttpUtil.get(sendUrl);
        log.info("结束调用远程服务{},返回结果为：{}", "getButton", postResult);
        return JSON.parseObject(postResult, AjaxResult.class);
    }

    @Override
    public PageResult<Map<String, Object>> getDoneTaskPage(Map<String, Object> paramsMap) {
        return null;
    }

    @Override
    public void updateTaskAssignee(Map<String, Object> paramsMap) {
    }


    @Override
    public AjaxResult approveTask(Map<String, Object> paramsMap) {
        log.info("开始调用远程服务{},参数为：{}", "approveTask", paramsMap);
        // 获取请求的地址
        String sendUrl = bpmConfig.getBaseUrl() + bpmConfig.getApproveTaskApi();
        String postResult = HttpUtil.post(sendUrl, JSON.toJSONString(paramsMap));
        log.info("结束调用远程服务{},返回结果为：{}", "approveTask", postResult);
        return JSON.parseObject(postResult, AjaxResult.class);
    }

    @Override
    public AjaxResult rejectTask(Map<String, Object> paramsMap) {
        log.info("开始调用远程服务{},参数为：{}", "rejectTask", paramsMap);
        // 获取请求的地址
        String sendUrl = bpmConfig.getBaseUrl() + bpmConfig.getRejectTaskApi();
        String postResult = HttpUtil.post(sendUrl, JSON.toJSONString(paramsMap));
        log.info("结束调用远程服务{},返回结果为：{}", "rejectTask", postResult);
        return JSON.parseObject(postResult, AjaxResult.class);
    }

    @Override
    public AjaxResult updateTaskVariable(Map<String, Object> paramsMap) {
        log.info("开始调用远程服务{},参数为：{}", "updateTaskVariable", paramsMap);
        // 获取请求的地址
        String sendUrl = bpmConfig.getBaseUrl() + bpmConfig.getUpdateTaskVariableApi();
        String postResult = HttpUtil.post(sendUrl, JSON.toJSONString(paramsMap));
        log.info("结束调用远程服务{},返回结果为：{}", "updateTaskVariable", postResult);
        return JSON.parseObject(postResult, AjaxResult.class);
    }
}
